/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:29
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2022-08-03 10:35:00
 */
/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:29
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2021-11-12 10:33:40
 */
package com.wxhandle.cleandemo.application.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.wxhandle.cleandemo.core.command.identity.RegisterUserCommand;
import com.wxhandle.cleandemo.core.domain.identity.SysClaim;
import com.wxhandle.cleandemo.core.domain.identity.SysRole;
import com.wxhandle.cleandemo.core.domain.identity.SysUser;
import com.wxhandle.cleandemo.core.domain.identity.SysService.ClaimType;
import com.wxhandle.cleandemo.core.services.SysUserService;
import com.wxhandle.cleandemo.persistence.repositorys.identity.SysUserRepository;


@Service
public class SysUserServiceImpl extends DomainServiceImpl<SysUser, Long> implements SysUserService {

    @Lazy
    @Autowired
    private SysUserRepository repository;

    /**
     * 注册用户
     */
    @Override
    public SysUser register(RegisterUserCommand command) {
        SysUser user = SysUser.ofRegister(command.getLogin(),command.getPwd(), command.getDescription(), 3);
        return repository.save(user);
    }

    @Override
    public SysUser getOneByLogin(String login) {
        SysUser user = repository.findLogin(login);
        return user;
    }

    @Override
    public List<SysClaim> getClaimsById(Long id, ClaimType type) {
        SysUser user = repository.getById(id);
        List<SysClaim> claims = new ArrayList<SysClaim>();
        for(SysRole role : user.getRoles()){
            claims.addAll(role.getClaims());
        }
        return claims;
    }

}
